//
//  MovieViewController.m
//  FNer
//
//  Created by dllo on 15/11/6.
//  Copyright © 2015年 刘志强. All rights reserved.
//

#import "MovieViewController.h"
#import "AFNTools.h"
#import "MovieCollectionViewCell.h"
#import "MovieModel.h"
#import "MovieCoreData.h"

#import "MovieListViewController.h"
#import "UINavigationController+animation.h"
@interface MovieViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, retain)NSMutableArray *httparray;
@property (nonatomic, retain)UICollectionView *myCollectionView;
@property (nonatomic,retain)NSMutableArray *allDataArray;
@property (nonatomic, assign)BOOL isUpLoading;
@end

@implementation MovieViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   self.allDataArray = [NSMutableArray array];
    
    self.title = @"视频";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UICollectionViewFlowLayout *flowlayOut = [[UICollectionViewFlowLayout alloc] init];
    flowlayOut.itemSize = CGSizeMake(self.view.frame.size.width, 150);
    flowlayOut.minimumInteritemSpacing = 0;
    flowlayOut.minimumLineSpacing = 0;
    flowlayOut.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-113) collectionViewLayout:flowlayOut];
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    //self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.myCollectionView  registerClass:[MovieCollectionViewCell class] forCellWithReuseIdentifier:@"Cell"];
    [self.view addSubview:self.myCollectionView];
    self.isUpLoading = YES;
    
    self.httparray = [NSMutableArray arrayWithObjects:@"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=创意&vc=75&t=MjAxNTA5MjgxMDM4NDE5NTMsOTUwMw%3D%3D&u=47d91bb040abf6c3e22042e2f21306855cb85bda&net=wifi&v=1.5.0&f=iphone",@"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=运动&vc=75&t=MjAxNTA5MjgxMDI3MjQ1MTYsMzU0Mw%3D%3D&u=47d91bb040abf6c3e22042e2f21306855cb85bda&net=wifi&v=1.5.0&f=iphone",@"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=旅行&vc=75&t=MjAxNTA5MjgxMDI4MjI0NzAsOTUzMw%3D%3D&u=47d91bb040abf6c3e22042e2f21306855cb85bda&net=wifi&v=1.5.0&f=iphone",@"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=剧情&vc=75&t=MjAxNTA5MjgxMDMwMzM0ODksMjg1OQ%3D%3D&u=47d91bb040abf6c3e22042e2f21306855cb85bda&net=wifi&v=1.5.0&f=iphone",@"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=动画&vc=75&t=MjAxNTA5MjgxMDMxMjM4MTUsNzU1MA%3D%3D&u=47d91bb040abf6c3e22042e2f21306855cb85bda&net=wifi&v=1.5.0&f=iphone",@"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=广告&vc=75&t=MjAxNTA5MjgxMDMyMDY4MDQsNTk3Nw%3D%3D&u=47d91bb040abf6c3e22042e2f21306855cb85bda&net=wifi&v=1.5.0&f=iphone",@"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=音乐&vc=75&t=MjAxNTA5MjgxMDM0MzQ2NzAsNjg4Ng%3D%3D&u=47d91bb040abf6c3e22042e2f21306855cb85bda&net=wifi&v=1.5.0&f=iphone",@"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=开胃&vc=75&t=MjAxNTA5MjgxMDM1NDE4MTAsNzQ5Nw%3D%3D&u=47d91bb040abf6c3e22042e2f21306855cb85bda&net=wifi&v=1.5.0&f=iphone",@"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=预告&vc=75&t=MjAxNTA5MjgxMDM2MzUwNDYsNjYxNg%3D%3D&u=47d91bb040abf6c3e22042e2f21306855cb85bda&net=wifi&v=1.5.0&f=iphone",@"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=综合&vc=75&t=MjAxNTA5MjgxMDM3MDczNDUsMTQ3OA%3D%3D&u=47d91bb040abf6c3e22042e2f21306855cb85bda&net=wifi&v=1.5.0&f=iphone",@"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=记录&vc=75&t=MjAxNTA5MjgxMDM3MDczNDUsMTQ3OA%3D%3D&u=47d91bb040abf6c3e22042e2f21306855cb85bda&net=wifi&v=1.5.0&f=iphone",@"http://baobab.wandoujia.com/api/v1/videos?num=10&categoryName=时尚&vc=75&t=MjAxNTA5MjgxMDM4NDE5NTMsOTUwMw%3D%3D&u=47d91bb040abf6c3e22042e2f21306855cb85bda&net=wifi&v=1.5.0&f=iphone", nil];
   

   
        NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
        NSEntityDescription *entity = [NSEntityDescription entityForName:@"MovieCoreData" inManagedObjectContext:[[CoreDataManager   shareManager] managedObjectContext]];
        [fetchRequest setEntity:entity];
       
        NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name"
                                                                       ascending:YES];
        [fetchRequest setSortDescriptors:[NSArray arrayWithObjects:sortDescriptor, nil]];
        
        NSError *error = nil;
        NSArray *fetchedObjects = [[[CoreDataManager shareManager] managedObjectContext] executeFetchRequest:fetchRequest error:&error];
      self.allDataArray = [NSMutableArray arrayWithArray:fetchedObjects];
       if (self.isUpLoading == YES) {
           
            [self.allDataArray removeAllObjects];
        
           for (MovieCoreData *mov in fetchedObjects)  {
               
               
               
               [[CoreDataManager shareManager].managedObjectContext deleteObject:mov];
              
           }
           [[CoreDataManager shareManager] saveContext];
           

        }
    
       
  
        // self.allDataArray = [NSMutableArray array];
        [self addHeaderRefresh];
  
    
}

- (void)addHeaderRefresh
{
    self.myCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.isUpLoading =YES;
        //    self.array = [NSMutableArray array];
        
        
        
        [self AFNGetData];
        
        
    }];
    
    [self.myCollectionView.header beginRefreshing];

}



-(void)AFNGetData
{
    NSString *url = [NSString stringWithFormat:@"http://baobab.wandoujia.com/api/v1/categories?vc=75&u=47d91bb040abf6c3e22042e2f21306855cb85bda&v=1.5.0&f=iphone"];
     [AFNTools getAFN:url andpara:nil andBlock:^(id result) {
         
    

         for (NSMutableDictionary *dic in result) {
            // MovieModel *model1 = [[MovieModel alloc] init];
             
             MovieCoreData *model = [NSEntityDescription insertNewObjectForEntityForName:@"MovieCoreData" inManagedObjectContext:[[CoreDataManager shareManager] managedObjectContext]];
             
             model.name = [dic objectForKey:@"name"];
             //[model setValuesForKeysWithDictionary:dic];
            model.bgPicture = [dic objectForKey:@"bgPicture"];
             [self.allDataArray addObject:model];
            
         }
         [[CoreDataManager shareManager] saveContext];
         [self.myCollectionView reloadData];
          [self.myCollectionView.header endRefreshing];
     } andfailBlock:^(id fail) {
         
          [self.myCollectionView.header endRefreshing];
     }];
    
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    //NSLog(@"array===%ld",self.allDataArray.count);
    return self.allDataArray.count;
    
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MovieCoreData *model = [self.allDataArray objectAtIndex:indexPath.row];
    NSString *reuse = @"Cell";
    MovieCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuse forIndexPath:indexPath];
    

    cell.model = model;
    return cell;
    
    
    
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MovieCoreData *model = [self.allDataArray objectAtIndex:indexPath.row];
    model.dataUrl = [self.httparray objectAtIndex:indexPath.row];
    
    MovieListViewController  *movlistVC = [[MovieListViewController alloc] init];
    movlistVC.model = model;
    
    

     [self.navigationController  pushViewController:movlistVC animatedWithTransition: UIViewAnimationTransitionFlipFromLeft];

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
